#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>

using namespace std;

//最大连续1的个数三
//昨天没做出来的关键原因在于left循环判断出窗口这一步
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {

        int n = nums.size();
        int count = 0;
        int ret = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            if (nums[right] == 0) count++;
            while (count > k)
            {
                if (nums[left] == 0) count--;
                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};

//将x减到0的最小操作数
//有两个细节错了好几次，第一个target<0的情况，第二个就是最后返回的时候返回的是差值，因为是用逆向思维去做的，返回值需要减一下
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        for (int e : nums) sum += e;
        int n = nums.size();
        int target = sum - x;
        if (target < 0) return -1;
        int ret = -1;
        int tmp = 0;

        for (int left = 0, right = 0; right < n; right++)
        {
            tmp += nums[right];
            while (tmp > target)
            {
                tmp -= nums[left++];
            }
            if (tmp == target) ret = max(ret, right - left + 1);

        }

        if (ret == -1) return  ret;
        else return n - ret;


    }
};
int main()
{


	return 0;
}